package com.jdd.modules.parkcar.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.common.api.vo.Result;
import com.jdd.common.exception.JddException;
import com.jdd.common.util.DateUtil;
import com.jdd.common.util.MonthlyUploadHttpUtils;
import com.jdd.modules.parkcar.entity.ParkBlackList;
import com.jdd.modules.parkcar.entity.ParkWhiteList;
import com.jdd.modules.parkcar.mapper.ParkBlackListMapper;
import com.jdd.modules.parkcar.service.IParkBlackListService;
import com.jdd.modules.parkmonthly.vo.AddMonthlySpace;
import com.jdd.modules.system.vo.ParkWhiteListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description: 黑名单
 * @author: jdd
 * @date: 2021年03月06日 16:03
 */
@Service
public class ParkBlackListServiceImpl extends ServiceImpl<ParkBlackListMapper, ParkBlackList> implements IParkBlackListService {
    @Resource
    private ParkBlackListMapper parkBlackListMapper;

    private  String parkAdminUrl;
    @Resource
    MonthlyUploadHttpUtils monthlyUploadHttpUtils;
    @Override
    @Transactional
    public void issueAddParkBlackListMessage(ParkWhiteListVO parkBlackListVO) {

        try {
            ParkBlackList parkWhiteList = new ParkBlackList();
            AddMonthlySpace addMonthlySpace = new AddMonthlySpace();
            String messageId = parkBlackListVO.getMessageId();
             addMonthlySpace.setStr(messageId);
            ParkBlackList byId = this.getById(parkBlackListVO.getId());
            BeanUtils.copyProperties(parkBlackListVO,parkWhiteList);
            parkWhiteList.setStartDate(parkBlackListVO.getStartTime());
            parkWhiteList.setEndDate(parkBlackListVO.getEndTime());
            parkWhiteList.setEndTime(parkBlackListVO.getEndDayTime());
            parkWhiteList.setStartTime(parkBlackListVO.getStartDayTime());
            if (null==byId){
                  this.save(parkWhiteList);
            }else {
                this.updateById(parkWhiteList);
            }
                Result result = monthlyUploadHttpUtils.postMonthlyUpload("", addMonthlySpace);
                if (200!=result.getCode()){
                    throw new JddException(500, result.getMessage());
            }
        }catch (Exception e){
            log.error("处理场端新增黑名单信息出现异常,进行数据回滚!");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            String messageId = parkBlackListVO.getMessageId();
            AddMonthlySpace addMonthlySpace = new AddMonthlySpace();
            addMonthlySpace.setStr(messageId);
            monthlyUploadHttpUtils.postMonthlyUpload("", addMonthlySpace);
        }

    }
    @Transactional
    @Override
    public void issueUpdateParkBlackListMessage(ParkWhiteListVO parkBlackListVO) {
        ParkBlackList parkWhiteList = new ParkBlackList();
        BeanUtils.copyProperties(parkBlackListVO,parkWhiteList);
        parkWhiteList.setStartDate(parkBlackListVO.getStartTime());
        parkWhiteList.setEndDate(parkBlackListVO.getEndTime());
        parkWhiteList.setEndTime(parkBlackListVO.getEndDayTime());
        parkWhiteList.setStartTime(parkBlackListVO.getStartDayTime());
        boolean saveSuccess=false;
        try {
            saveSuccess = this.updateById(parkWhiteList);
            String messageId = parkBlackListVO.getMessageId();
            AddMonthlySpace addMonthlySpace = new AddMonthlySpace();
            addMonthlySpace.setJudge(false);
            addMonthlySpace.setStr(messageId);
            if (saveSuccess){
                Result result = monthlyUploadHttpUtils.postMonthlyUpload("", addMonthlySpace);
                if (200!=result.getCode()){
                    throw new JddException(500, result.getMessage());
                }
            }
        }catch (Exception e){
            log.error("处理场端修改黑名单信息出现异常,进行数据回滚!");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            String messageId = parkBlackListVO.getMessageId();
            AddMonthlySpace addMonthlySpace = new AddMonthlySpace();
            addMonthlySpace.setStr(messageId);
            monthlyUploadHttpUtils.postMonthlyUpload("", addMonthlySpace);
        }
    }

    @Override
    public void delectById(String id) {
        parkBlackListMapper.deleteById(id);
    }
}
